//var urlfile = "http://127.0.0.1:8080/jgj/";
var urlfile = "http://jgj.hzlingdian.com/jgj/";

var packageName = "jgj_";

function dataSave (key, value) {
	localStorage.setItem(packageName + key, value);
}

function JdataSave (key, value) {
	localStorage.setItem(packageName + key, JSON.stringify(value));
};

function dataGet (key) {
	return localStorage.getItem(packageName + key);
}

function JdataGet (key) {
	return JSON.parse(localStorage.getItem(packageName + key));
}

function dataRemove (key) {
	localStorage.removeItem(packageName + key);
}

function dataClear (argument) {
	localStorage.clear();
}

function refreshPage() {
	window.location.reload();
};

function backPage() {
	history.back();
}

function backGo(index) {
	window.history.go(index);
}

function stopDeliverEvent() {
	event.stopPropagation();
}

function ajaxTool(type,data,url,error,success,hideLogin) {
	if(!hideLogin) {
		layer.open({type: 2});
	}
	$.ajax({
		data: data,
		type: type,  
	    url: url,
		error:function(XMLHttpRequest, textStatus, errorThrown,fnErr){
			  var errorno = XMLHttpRequest.readyState;
	            var oMessage = {
	                "timeout": errorno+"请求超时",
	                "error": errorno+"请求超时",
	                "notmodified": errorno+"请求超时",
	                "parsererror": errorno+"数据格式出错"
	            };
	            if(!hideLogin) {
		            layer.closeAll();
		        }

	            if(fnErr){
	                fnErr();
	                return;
	            }
	            if(!textStatus && errorThrown){
	                alert(errorThrown);
	            }
	            if(textStatus){
	                switch (textStatus) {
	                    case "timeout":
	                        alert(oMessage.timeout);
	                        break;
	                    case "parsererror":
	                        alert(oMessage.parsererror);
	                        break;
	                    default:
	                        break;
	                }
	            }
			error(XMLHttpRequest, textStatus, errorThrown,fnErr);
		},
		success:function(data){
			if(!hideLogin) {
	            layer.closeAll();
	        }
			success(data);
		}
	});
}

//字符串日期格式yyyy-MM-dd
var getDateFormat = function (str){
	var date = str.substr(0,4)+"-"+str.substr(4,2)+"-"+str.substr(6,2);
	return date;
};
//字符串时间格式 hh:mm:ss
var getTimeFormat = function (str){
	var diff = 6-str.length;
	var time = str;
	for(var j=0;j<diff;j++){
		time = "0"+time;
	}
	time =  time.substr(0,2)+":"+time.substr(2,2)+":"+time.substr(4,2);
	return time;
};
//字符串日期时间格式 yyyy-MM-dd hh:mm:ss
var getDateTimeFormat = function (str){
	var datetime =  str.substr(0,4)+"-"+str.substr(4,2)+"-"+str.substr(6,2)+' '+str.substr(8,2)+":"+str.substr

(10,2)+":"+str.substr(12,2);
	return datetime;
};
//验证手机号码
var IsMobile = function (text){
    var _emp = /^\s*|\s*$/g;
    text = text.replace(_emp, "");
    var _d = /^1[3578][01379]\d{8}$/g;
    var _l = /^1[34578][01256]\d{8}$/g;
    var _y = /^(134[012345678]\d{7}|1[34578][012356789]\d{8})$/g;
    if (_d.test(text) || _l.test(text) || _y.test(text)) {
        return 1;
    }
    return 0;
};
//限制输入11位数字
var chekNum = function (obj){
    obj.val(obj.val().replace(/[^\d.]/g,""));
    obj.val(obj.val().replace(/^\./g,""));
    obj.val(obj.val().replace(/\.{2,}/g,"."));
    obj.val(obj.val().replace(".","$#$").replace(/\./g,"").replace("$#$","."));
    if(obj.val().length > 11){
    	obj.val(obj.val().substring(0,11));      
    }
};
/*限制输入字数
 * @minwidth :最小字数;
 * @maxwidth :最大字数;
 * @thistext :所选的输入框;
*/
function limitInput(minwidth,maxwidth,thistext){
	if(thistext.val().length>maxwidth){
		thistext.val(thistext.val().substring(0,maxwidth));
		alert("不能超过"+maxwidth+"个！");
	}
	if(thistext.val().length<minwidth){
		alert("不能少于"+minwidth+"个！");
		thistext.focus();
		return 0;
	}
}
/*限制输入字数
 * @maxwidth :最大字数;
 * @thistext :所选的输入框;
*/
function limitMaxInput(maxwidth,thistext){
	if(thistext.val().length>maxwidth){
		thistext.val(thistext.val().substring(0,maxwidth));
		alert("不能超过"+maxwidth+"个！");
	}
}

//获得url参数
function GetParpam(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

//页面跳转
function nextView(url) {
	location.href=url;
}
//获取url?后参数的值
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return decodeURI(r[2]); return null; 
}

//控制整数或小数点后两位  页面样式<input type="text" onkeyup="clearNoNum(this)" onpaste="clearNoNum(this)"/>
function clearNoNum(obj){
	obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
	obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字而不是
	obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
}

// 对Date的扩展，将 Date 转化为指定格式的String   
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，   
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
// 例子：   
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
Date.prototype.Format = function(fmt) {
  var o = {   
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日   
    "h+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}; 

var Validator = {
	phoneNo: /^\d{8,11}$/,
	mobileNo: /^1[3|4|5|8][0-9]\d{8}$/,
	bankCardNo: /^\d{16,19}$|^\d{6}[- ]\d{10,13}$|^\d{4}[- ]\d{4}[- ]\d{4}[- ]\d{4,7}$/,
	email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	realName: /^[\u4e00-\u9fa5]{2,5}$/,
	idCard: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
	medicalCardNo:/^[0-9a-zA-Z]+$/,
	num_en: /^[0-9a-zA-Z]{6,16}$/,
	letter: /^[a-zA-Z]+$/,
	ip: /(\d+\.){3}\d+/,
	stringIsMobileNo: function(s) {
		return this.mobileNo.test(s);
	},
	stringIsIdCard: function(s) {
		return this.idCard.test(s);
	},
	stringIdBankCardNo: function(s) {
		return this.bankCardNo.test(s);
	},
	stringContainsSpace: function(s) {
		return (/\s/).test(s);
	},
	stringIsNumOrEn:function (s) {
		return this.num_en.test(s);
	},
	isValidIp: function(ip) {
		var result = this.ip.test(ip);
		if (result) {
			var ips = ip.split('.');
			for (var i = 0; i < ips.length; ++i) {
				var a = ips[i];
				if (a-0 > 255 && ('' + (a-0) === a)) {
					result = false;
					break;
				}
			}
		}
		return result;
	},
	isValidIpPort: function(ipport) {
		var idx = ipport.indexOf(':');
		if (idx > -1) {
			var ipp = ipport.split(':'),
				ip = ipp[0],
				port = ipp[1],
				portNum = port-0;
			if (/\d+/.test(port) && (portNum > 0 && portNum <= 65535) && ('' + portNum === port))
				return this.isValidIp(ip);
		} else {
			return this.isValidIp(ipport);
		}
		return false;
	}
};

 function toUTF8(szInput)
{ 
     var wch,x,uch="",szRet="";
     for (x=0; x<szInput.length; x++)
     {
      wch=szInput.charCodeAt(x);
      if (!(wch & 0xFF80))
      {
       szRet += szInput.charAt(x);
      }
      else if (!(wch & 0xF000))
      {
       uch = "%" + (wch>>6 | 0xC0).toString(16) + "%" + (wch & 0x3F | 0x80).toString(16);
       szRet += uch; 
       }
      else
      {
       uch = "%" + (wch >> 12 | 0xE0).toString(16) + "%" + (((wch >> 6) & 0x3F) | 0x80).toString(16) +"%" + (wch & 0x3F | 0x80).toString(16);
       szRet += uch; 
      }
     }
     return(szRet);
}

function validSession() {
	var now = (new Date()).Format("yyyy/MM/dd hh:mm:ss");
	var oldTime = dataGet("nowTime");
	if(!oldTime){
		return;
	}
	var days = timeCompare(now, oldTime, 'd');
	if(days>3){
		dataClear();
	}

}

//用正则,计算时间差, 且精确到秒 type:d,h,m,s
function timeCompare(day1, day2, type){
    var y1, y2, y3, m1, m2, m3, d1, d2, d3, h1, h2, h3, _m1, _m2, _m3, s1, s2, s3;
    var reg = /年|月|日 |\/|:| /;
    //dayinfo -  用正则分割
    var DI1 = day1.split(reg);
    var DI2 = day2.split(reg);
    var date1 = new Date(DI1[0], DI1[1], DI1[2], DI1[3], DI1[4], DI1[5]);
    var date2 = new Date(DI2[0], DI2[1], DI2[2], DI2[3], DI2[4], DI2[5]);
    //用距标准时间差来获取相距时间
    var minsec = Date.parse(date1) - Date.parse(date2);
    var timeshort;
    if(type == "s"){
    	timeshort = minsec / 1000;
    }
    if(type == "m"){
    	timeshort = minsec / 1000 / 60;
    }
    if(type == "h"){
    	timeshort = minsec / 1000 / 60 / 60;
    }
    if(type == "d"){
    	timeshort = minsec/ 1000 / 60 / 60 / 24;
    }

    return timeshort;
}
/** 
* 判断是否null 
* @param data 
*/ 
function isNull(data){ 
	if(data == "" || data == undefined || data == null)
	{
		return true;
	}
	else
	{
		return false;
	}
}